package com.common.controller;

import com.common.entity.DirectDownloadResponse;
import com.common.service.FileDownloadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping
public class FileDownloadController {

    @Autowired
    private FileDownloadService fileDownloadService;

    @GetMapping("/{value}")
    public ResponseEntity<Resource> downloadFile(@PathVariable("value") String value) {
        // 调用文件下载服务获取文件资源
        Resource resource = fileDownloadService.loadFileAsResource(value);
        //确定文件类型
        String contentType = "application/octet-stream";
        // 返回文件资源作为响应
        return ResponseEntity.ok()
                .contentType(MediaType.parseMediaType(contentType))
//                .contentType(MediaType.ALL)
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
                .body(resource);
    }
}
